/*
  (C) 2005 Adam D. Bradley

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package step;

import java.util.ArrayList;
import step.exceptions.IllegalChildException;
import step.exceptions.STEPException;

public class SpliceNode extends ReferenceNode
{
    public SpliceNode(String id, String target) 
    {
	super(id);
	Target = target;
    }

	public SpliceNode(String target) {
		super();
		Target = target;
	}

	public String GetTarget() { return Target; }

	protected void Bind() { assert(false); } /* This must never happen. */

	protected String Target;

	public void Evaluate() throws step.EvaluationFailure {
		rt_evaluated = true;
		throw new step.EvaluationFailure(
			this, 
			"Splice nodes are supposed to be ephemeral " + 
			"and should not be evaluated!\n" + 
			"Dangling Splice: SpliceNode [" + 
                        GetID() + 
                        "] targets non-extant ID [" + 
                        GetTarget() + "]");
	}

	public String TagName() { return "splice"; }
	public void Write(java.io.OutputStream out, int depth) throws java.io.IOException {
		WriteBasis(out, depth, "target=\"" + Target + "\"");
	}
	
	// this code is added by ray
	//	 MODIFIES: compilationErrors 
	// EFFECT: checks if Teget node exists , if not it adds an exception to  compilationErrors

	public void validate(step.Graph g,
			ArrayList<STEPException> compilationErrors) {
		
		if(this.GetAllAncestorsCycleSafe().containsKey(Target)){
			compilationErrors.add(new IllegalChildException(
					"Splice node with ID = " + ID
							+ " has a target that is its own ancestor. Unacceptable. "));
		}
		if (g.GetNode(Target) == null)
			compilationErrors.add(new IllegalChildException(
					"Splice node with ID = " + ID
							+ " specifies a non-existant target. "));

	}
	
	@Override
	public Node ShallowCopy() {
		SpliceNode cn = new SpliceNode(ID, Target);
		Node.CopyNonRTPropertiesFromAtoB(this,cn);
		return cn;
	}
}
